package com.smooware.icms.fragment.navigation;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.smooware.icms.fragment.FragmentNavigatorHelper;
import com.smooware.icms.fragment.sync.AuthorizationFragment;

public abstract class AbstractFragmentNavigator {

	protected final FragmentNavigatorHelper navigatorHelper;

	protected final FragmentActivity holder;

	public AbstractFragmentNavigator(final FragmentActivity holder) {
		navigatorHelper = new FragmentNavigatorHelper(holder);

		this.holder = holder;
	}

	/**
	 * Navigates to {@link AuthorizationFragment}. Adds it if there is no
	 * fragment shown at the moment, or replace it if there is non
	 * {@link AuthorizationFragment} instance shown at the moment.
	 */
	public void goToAuthorizationFragment() {
		if (holder.getSupportActionBar().isShowing()) {
			holder.getSupportActionBar().hide();
		}

		Fragment fragment = new AuthorizationFragment();

		if (navigatorHelper.isFragmentFrameEmpty()) {
			navigatorHelper.addFragment(fragment);

		} else if (!(navigatorHelper.getCurrentFragment() instanceof AuthorizationFragment)) {
			navigatorHelper.replaceFragment(fragment);
		}
	}

	/**
	 * Checks if {@link AuthorizationFragment} is shown at the moment.
	 * 
	 * @return true if {@link AuthorizationFragment} is shown at the moment,
	 *         false otherwise.
	 */
	public boolean isLockedByAuthorization() {
		return navigatorHelper.getCurrentFragment() instanceof AuthorizationFragment;
	}

	/**
	 * Finishes {@link AuthorizationFragment} and navigates to main fragment of
	 * the activity.
	 */
	public abstract void processAuthorizationFragment();
}
